/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package myprogramui.comp.page2comp;

import Globals.AllGlobal;
import brain.MyBrain;
import controller.MyController;
import database.operate.OperateMyDatabaseImpl;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.embed.swing.JFXPanel;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.control.SliderBuilder;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.effect.Shadow;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.shape.LineBuilder;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.text.TextBuilder;
import javafx.util.Callback;
import model.GongZuoMian_Model;
import model.ZuanChang_Model;
import model.ZuanKong_Model;
import myfx.component.TextFieldTreeCellImpl;
import myprogramui.comp.DrillPage;

/**
 *
 * @author Administrator
 */
public class FXTreeForJME extends JFXPanel{
    private int width;
    private int height;
    private MyController controller;
    private MyBrain brain;
    private Color color;
    private RightTree tree;
    public FXTreeForJME(int width,int height,Color color){
        this.width=width;
        this.height=height;

        this.color=color;
        this.setPreferredSize(new Dimension(width, height));
        
        tree=new RightTree(controller, brain);
        tree.setPrefSize(width, height);
        createMyTreeContainer();
    }
    public void createMyTreeContainer(){
        Group group=new Group();
         
        Line splitLine= LineBuilder.create()
                .startX(0)
                .endX(width-6)
                .startY(0)
                .endY(0)
                .stroke(Color.LIGHTGRAY)
                .strokeWidth(1)
                .build();
        
        Line splitShuLine= LineBuilder.create()
                .startX(0)
                .endX(0)
                .startY(0)
                .endY(55)
                .stroke(Color.LIGHTGRAY)
                .strokeWidth(1)
                .build();
        
        Text TiaoJieMeiCengQingJiaoText= TextBuilder.create()
                .text("调节煤层倾角")
                .x(0)
                .y(0)
                .font(new Font(14))
                .build();
        
        final Text DangQianQJ= TextBuilder.create()
                .text("当前煤层倾角:")
                .x(0)
                .y(0)
                .font(new Font(12))
                .fill(color.RED)
                .build();
        
        final Slider qingJiaoSlider = SliderBuilder.create() 
                .min(0) 
                .max(90) 
                .value(0) 
                .showTickLabels(true) 
                .showTickMarks(true) 
                .minorTickCount(10) 
                .blockIncrement(1) 
                .build(); 
        
        TiaoJieMeiCengQingJiaoText.setTranslateY(24);
        TiaoJieMeiCengQingJiaoText.setTranslateX(12);
        qingJiaoSlider.setTranslateY(30);
        splitLine.setTranslateY(65);
        splitLine.setTranslateX(3);
        splitShuLine.setTranslateX(150);
        splitShuLine.setTranslateY(5);
        DangQianQJ.setTranslateX(160);
        DangQianQJ.setTranslateY(24);
        
        qingJiaoSlider.setOnMouseClicked(new EventHandler<MouseEvent>() {

            public void handle(MouseEvent t) {
                double f=qingJiaoSlider.getValue();
                String s=String.valueOf(f);
                int index=s.indexOf(".");
                index+=2;
                String subString=s.substring(0, index);
                DangQianQJ.setText("当前煤层倾角:\n"+subString);
                float ffF=Float.valueOf(subString);
                controller.setMEICENGQINGJIAOF(ffF);
            }
        });
        
        tree.setTranslateY(75);
        
        group.getChildren().add(TiaoJieMeiCengQingJiaoText);
        group.getChildren().add(qingJiaoSlider);
        group.getChildren().add(splitLine);
        group.getChildren().add(splitShuLine);
        group.getChildren().add(DangQianQJ);
        
        tree.setCellFactory(new Callback<TreeView<String>, TreeCell<String>>() {

            public TreeCell<String> call(TreeView<String> p) {
                return new TextFieldTreeCellImpl(controller,brain);
            }
        });
        tree.setOnMouseEntered(new EventHandler<MouseEvent>() {

            public void handle(MouseEvent t) {
                tree.requestFocus();
            }
        });
        
        group.getChildren().add(tree);
        tree.setPrefSize(width, height-75);
        tree.setEditable(true);
        tree.setOpacity(0.73);
        Scene scene = new Scene(group,width,height,color);
        this.setScene(scene);
    }
    public void addController(MyController controller){
        this.controller=controller;        
    }
    public void addBrain(MyBrain brain){
        this.brain=brain;
    }
    public TreeView<String> getTree() {
        return tree;
    }

    public void setTree(RightTree tree) {
        this.tree = tree;
    }
    
    public boolean fillTree(){    
        
        try {
            ArrayList<GongZuoMian_Model> gzmlist=OperateMyDatabaseImpl.getAllResult(AllGlobal.GONGZUOMIAN_SQL);
            ArrayList<ZuanChang_Model> zclist=OperateMyDatabaseImpl.getAllResult(AllGlobal.ZUANCHANG_SQL);
            TreeItem<String> root=tree.getRoot();
            TreeItem<String> gzmItem=null;
            if(gzmlist.size()>0){
                gzmItem=new TreeItem<String>(gzmlist.get(0).getMingcheng());
                root.getChildren().add(gzmItem);
            }
            for(int i=0;i<zclist.size();i++){
                ZuanChang_Model zcm=zclist.get(i);
                TreeItem<String> zcItem=new TreeItem<String>(zcm.getMingcheng());
                gzmItem.getChildren().add(zcItem);
                ArrayList<ZuanKong_Model> zklist_tmp=OperateMyDatabaseImpl
                        .getAllResult(AllGlobal.ZUANKONG_SQL,"select * from zuankong where zuanchang='"+zcm.getMingcheng()+"';");
                for(int j=0;j<zklist_tmp.size();j++){
                    ZuanKong_Model zkm=zklist_tmp.get(j);
                    TreeItem<String> zkItem=new TreeItem<String>(zkm.getMingcheng());
                    zcItem.getChildren().add(zkItem);
                }
            }
            if (zclist.size()<1) {
                return false;
            }
        } catch (Exception ex) {
            Logger.getLogger(DrillPage.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return true;
    }
}
